/**
 * 
 */
package com.onjava.scripting.scriptobjs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.PlatformUI;

/**
 * A sample scriptedObject which exposes a file selection dialog. 
 * 
 * @author battlehorse
 *
 */
public class FileSelector {
	
	private String f;

	/**
	 * Opens a file selection dialog and returns the selected file.
	 * <b>Note:</b> since we are calling {@code syncExec}, this method must not be invoked inside a modal script
	 * 
	 * @return
	 */
	public String selectFile() {
		Display d = PlatformUI.getWorkbench().getDisplay();
		d.syncExec(new Runnable() {
			public void run() {
				FileDialog dlg = new FileDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell(),
						SWT.OPEN);		
				
				f = dlg.open();
				
			}
		});
					

		return f; 	
	}
}
